package cn.com.fuhuitong.main.home.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.base.BaseViewModel
import cn.com.fuhuitong.base.SafeMutableLiveData
import cn.com.fuhuitong.http.BaseResponse
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.http.ViewModelSubscribeListener
import cn.com.fuhuitong.main.home.entity.CharityDetailsResponse
import cn.com.fuhuitong.main.home.entity.CharityListResponse
import cn.com.fuhuitong.main.home.entity.CharitySignUpEntity
import cn.com.fuhuitong.main.home.mode.HomeManager
import io.reactivex.disposables.Disposable

class CharityViewModel(application: Application) : BaseViewModel(application) {
    private val homeManager: HomeManager = HomeManager()

    val charityListDataLiveData = MutableLiveData<HttpResponse<CharityListResponse>>()

    val charityDetailsDataLiveData = MutableLiveData<HttpResponse<CharityDetailsResponse>>()

    val baseDataLiveData = MutableLiveData<HttpResponse<BaseResponse>>()


    private var page: Int = 1
    private var limit: Int = 100

    /**
     * 获取公益慈善列表
     */
    fun charityIndex(type: Int) {
        val listener =
            object : ViewModelSubscribeListener<CharityListResponse>(charityListDataLiveData) {}
        val charityIndexDisposable: Disposable = homeManager.charityIndex(
            BaseApplication.userToken(),
            page.toString(), limit.toString(),
            type.toString(), listener
        )
        dispose.add(charityIndexDisposable)
    }

    /**
     * 获取公益慈善详情
     */
    fun charityDetails(charityId: Int) {
        val listener =
            object :
                ViewModelSubscribeListener<CharityDetailsResponse>(charityDetailsDataLiveData) {}
        val charityDetailsDisposable: Disposable = homeManager.charityDetails(
            BaseApplication.userToken(), charityId.toString(), listener
        )
        dispose.add(charityDetailsDisposable)
    }

    /**
     * 获取公益慈善报名
     */
    fun charitySignUp(signUpEntity: CharitySignUpEntity) {
        val listener =
            object :
                ViewModelSubscribeListener<BaseResponse>(baseDataLiveData) {}
        val charitySignUpDisposable: Disposable = homeManager.charitySignUp(
            signUpEntity, listener
        )
        dispose.add(charitySignUpDisposable)
    }
}